let NumberAnimate = require("../../utils/NumberAnimate");
var app = getApp();

Page({

  data: {
    free_num: "",
    park_infos: "",
    indexview: "",
    animationNavigationItem: "",
    floatOne: false,
    floatTwo: true
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '华元车场空位查询',
    })

    // 判断是否第一次打开，是否启动引导页
    if (wx.getStorageSync("flag_parkInfo") == true) {
      this.setData({
        floatOne: true,
        floatTwo: true
      })
    } else {
      app.globalData.flag_parkInfo = true;
      wx.setStorageSync("flag_parkInfo", app.globalData.flag_parkInfo);
    }

    // 获取数据
    let indexview = options.indexview;
    this.setData({
      park_infos: app.globalData.park_list[indexview],
      indexview: indexview
    })
    if (this.data.park_infos.free == "") {
      this.getParkData();
    }

    this.animate(indexview);
    if(this.data.free_num == ""){
      this.setData({
        free_num: app.globalData.park_list[indexview].free
      })
    } else {
      this.animate(indexview);
    }
    // 心跳动画效果
    var circleCount = 0;
    this.animationNavigationItem = wx.createAnimation({
      timingFunction: 'linear',
      delay: 100,
      transformOrigin: '50% 50%',
      success: function (res) {
      }
    });
    setInterval(function () {
      if (circleCount % 2 == 0) {
        this.animationNavigationItem.scale(1.1).step();
      } else {
        this.animationNavigationItem.scale(1.0).step();
      }
      this.setData({
        animationNavigationItem: this.animationNavigationItem.export()
      });
      circleCount++;
      if (circleCount == 1000) {
        circleCount = 0;
      }
    }.bind(this), 1000);
  }, 

  // 数字递增动画效果
  animate: function (current) {
    this.setData({
      free_num: ""
    });
    let free_num = app.globalData.park_list[current].free;
    let n = new NumberAnimate({
      from: free_num,
      speed: 2000,
      refreshTime: 100,
      onUpdate: () => {
        this.setData({
          free_num: n.tempValue
        });
      },
    });
  },

  // 分享小程序
  onShareAppMessage: function (ops) {
    var that = this;
    var indexview = that.data.indexview;
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '华元车场空位查询',
      desc: '',
      path: 'pages/parkInfo/parkInfo?indexview=' + indexview,
      success: function (res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
        that.getParkData();
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  },

  floatOne: function () {
    this.setData({
      floatOne: true,
      floatTwo: false
    })
  },

  floatTwo: function () {
    this.setData({
      floatTwo: true
    })
  },

  getParkData: function () {
    var param = [`${app.globalData.park_list[0].parkId}`, `${app.globalData.park_list[1].parkId}`, `${app.globalData.park_list[2].parkId}`];
    // console.log(param);
    const that = this;
    wx.request({
      url: 'https://manager.zjheadway.cn/parking/getExtra?parkId=' + param,
      data: JSON.stringify(param),
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        for (var index = 0; index < app.globalData.park_list.length; index++) {
          app.globalData.park_list[index] = Object.assign(app.globalData.park_list[index], res.data.value[index])
        }
        that.setData({
          park_infos: app.globalData.park_list[that.data.indexview]
        });
        app.globalData.onlanch = true;
      }
    })
  },

  goOutUrl: function(){
    wx.navigateTo({
      url: '../outurl/outurl',
    })
  },

  goToRoute: function () {
    let url = '../routes/routes?indexview=' + this.data.indexview;
    console.log(url);
    wx.navigateTo({
      url: url,
    });
  }
})